/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-17 14:00:30  
 * Copyright (c) 2015- -版权所有     
 */
package com.joysuch.wwyt.edu.service.impl;

import javax.transaction.Transactional;


import com.joysuch.wwyt.edu.bean.EduCourseStudyHistoryDetailBean;
import com.joysuch.wwyt.edu.bean.EduCourseStudyHistoryEditBean;
import com.joysuch.wwyt.edu.bean.EduCourseStudyHistoryListBean;
import static com.joysuch.wwyt.edu.bean.EduCourseStudyHistoryListBean.SearchEduCourseStudyHistory;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.edu.repository.EduCourseDao;
import com.joysuch.wwyt.edu.repository.EduCoursewareDao;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;

import com.querydsl.core.types.Predicate;
import com.joysuch.wwyt.edu.entity.EduCourseStudyHistory;
import com.joysuch.wwyt.edu.entity.dsl.QEduCourseStudyHistory;
import com.joysuch.wwyt.edu.repository.EduCourseStudyHistoryDao;
import com.joysuch.wwyt.edu.service.EduCourseStudyHistoryService;

import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


@Service
@Transactional
public class EduCourseStudyHistoryServiceImpl implements EduCourseStudyHistoryService {

    @Autowired
    private EduCourseStudyHistoryDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private EduCourseDao eduCourseDao;
    @Autowired
    private EduCoursewareDao eduCoursewareDao;
    
    @Override
    public EduCourseStudyHistory add(EduCourseStudyHistoryEditBean entity) {
        EduCourseStudyHistory content  = new EduCourseStudyHistory();
        BeanUtils.copyProperties(entity, content, "id");
        return dao.save(content);
    }

    @Override
    public void update(EduCourseStudyHistoryEditBean entity) {
        EduCourseStudyHistory content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
    }

    @Override
    public Page<EduCourseStudyHistoryListBean> findByPage(Pageable page , SearchEduCourseStudyHistory condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<EduCourseStudyHistory> data =  predicate == null? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data, new PageDataConvert<EduCourseStudyHistoryListBean, EduCourseStudyHistory>() {

            @Override
            public EduCourseStudyHistoryListBean convert(EduCourseStudyHistory f) {
                EduCourseStudyHistoryListBean bean = new EduCourseStudyHistoryListBean();
                BeanUtils.copyProperties(f, bean);
            bean.setUserName(baseUserDao.getRealNameById(f.getUserId()));
            bean.setCourseName(eduCourseDao.getNameById(f.getCourseId()));
            bean.setCoursewareName(eduCoursewareDao.getNameById(f.getCoursewareId()));
                return bean;
            }

        });
    }

    private Predicate  prepareListSearchCondition(SearchEduCourseStudyHistory condition) {
        Predicate p = null;
        QEduCourseStudyHistory q = QEduCourseStudyHistory.eduCourseStudyHistory;
        if (condition.getUserId() != null) {
            p = ExpressionUtils.and(p, q.userId.eq(condition.getUserId()));
        }
        if (condition.getCourseId() != null) {
            p = ExpressionUtils.and(p, q.courseId.eq(condition.getCourseId()));
        }
        if (condition.getCoursewareId() != null) {
            p = ExpressionUtils.and(p, q.coursewareId.eq(condition.getCoursewareId()));
        }
        if (condition.getCoursewareType() != null) {
            p = ExpressionUtils.and(p, q.coursewareType.eq(condition.getCoursewareType()));
        }
        if (condition.getStartTime() != null) {
            p = ExpressionUtils.and(p, q.startTime.eq(condition.getStartTime()));
        }
        if (condition.getLengthMinute() != null) {
            p = ExpressionUtils.and(p, q.lengthMinute.eq(condition.getLengthMinute()));
        }
        if (condition.getCurrentProgress() != null) {
            p = ExpressionUtils.and(p, q.currentProgress.eq(condition.getCurrentProgress()));
        }
        if (condition.getCurrentProgressRatio() != null) {
            p = ExpressionUtils.and(p, q.currentProgressRatio.eq(condition.getCurrentProgressRatio()));
        }
        if (condition.getStudyContentLength() != null) {
            p = ExpressionUtils.and(p, q.studyContentLength.eq(condition.getStudyContentLength()));
        }
        return p;
    }
    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        //校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * @param id 要删除的业务ID
     * @throws BussinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public EduCourseStudyHistory findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public EduCourseStudyHistoryDetailBean getById(Long id) {
        EduCourseStudyHistory entity = findById(id) ;
        if(entity == null) {
            return null;
        }
        EduCourseStudyHistoryDetailBean bean = new EduCourseStudyHistoryDetailBean();
        BeanUtils.copyProperties(entity, bean);
            bean.setUserName(baseUserDao.getRealNameById(entity.getUserId()));
            bean.setCourseName(eduCourseDao.getNameById(entity.getCourseId()));
            bean.setCoursewareName(eduCoursewareDao.getNameById(entity.getCoursewareId()));
        return bean;
    }

}
